package com.bangyu.monitor.core.util;


import com.google.common.collect.Lists;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: zhangxue
 * @Date: 19/7/4 22:55
 * @Description:
 */
public class ReadBin {
    public static void main(String[] args) throws IOException {
        File file = new File("/home/pvc/Desktop/0104711906171_20190704.bin");

        InputStream in = new  FileInputStream(file);
        byte[] payLoad = new byte[322];
        in.read(payLoad);



        byte[] tdBytes = Arrays.copyOfRange(payLoad,0,320);//通道数据
        byte[] temperatureBytes = Arrays.copyOfRange(payLoad,320,322);//温度
        Integer temperatureInt = bytes2Int(temperatureBytes);
        Float temperatureFloat = (new BigDecimal(temperatureInt).divide(new BigDecimal(100))).floatValue();

        //将二进制数据转换成160组位移数据
        List<Float> floats = Lists.newArrayList();
        for(int i=0;i<160;i++){
            int ivalue1 = bytes2Int(Arrays.copyOfRange(tdBytes,i*2,(i+1)*2));
            float fvalue = trans2Displayment(ivalue1);
            floats.add(new BigDecimal(fvalue).setScale(5, RoundingMode.HALF_DOWN).floatValue());
        }
        List<List> twentyGroups = Lists.newArrayList();
        for(int j=0;j<10;j++){
            List<Float> list = floats.subList(j*16,(j+1)*16);
            List list1 = Lists.newArrayList();
            List list2 = Lists.newArrayList();
            for(int k=0;k<list.size();k++){


                if(k%2==0){
                    list1.add(list.get(k));
                }else{
                    list2.add(list.get(k));
                }
            }
            twentyGroups.add(list1);
            twentyGroups.add(list2);
        }

        for(List l:twentyGroups){
            System.out.println(String.format("%s,%s,%s,%s,%s,%s,%s,%s",l.get(0),l.get(1),l.get(2),l.get(3),l.get(4),l.get(5),l.get(6),l.get(7)));

        }
        System.out.println("temper:"+temperatureFloat);



     /*   byte[] tdBytes = Arrays.copyOfRange(bytes,0,320);
        byte[] temperatureBytes = Arrays.copyOfRange(bytes,320,322);

        List<Float> list = Lists.newArrayList();
        for(int i=0;i<160;i++){
            int ivalue1 = bytes2Int(Arrays.copyOfRange(tdBytes,i*2,(i+1)*2));
            float fvalue = trans2Displayment(ivalue1);
            list.add(fvalue);
        }
        List<List> list4 = Lists.newArrayList();
        for(int j=0;j<10;j++){
            List<Float> list1 = list.subList(j*16,(j+1)*16);
            List list2 = Lists.newArrayList();
            List list3 = Lists.newArrayList();
            for(int k=0;k<list1.size();k++){


                if(k%2==0){
                    list2.add(list1.get(k));
                }else{
                    list3.add(list1.get(k));
                }
            }
            list4.add(list2);
            list4.add(list3);
        }
        for(List l:list4){
            System.out.println(String.format("%s,%s,%s,%s,%s,%s,%s,%s",l.get(0),l.get(1),l.get(2),l.get(3),l.get(4),l.get(5),l.get(6),l.get(7)));
        }*/


    }


    private static Integer bytes2Int(byte[] bytes){
        return ((bytes[0] & 0xFF)
                | (bytes[1] & 0xFF) << 8);
        //        return byte_int*bytes[0]+bytes[1];
    }


    private static float trans2Displayment(Integer byteValue){
        BigDecimal byteValueDeci = new BigDecimal(byteValue);
        BigDecimal voltageDeci = byteValueDeci.multiply(new BigDecimal(5)).divide(new BigDecimal(32768),4,BigDecimal.ROUND_HALF_UP);
        BigDecimal result =  new BigDecimal(25).subtract(voltageDeci.multiply(new BigDecimal(5)));
        return result.floatValue();
    }



}
